package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.pojo.controlspec.ToggleButtonSpec;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class ToggleFormat {
	public static final String DISABLE_GROUP = "disableGroup";
	public static final String IS_CHECKED = "isChecked";
	public static final String TEXT_OFF = "textOff";
	public static final String TEXT_ON = "textOn";
	public static final String TOGGLE_AUTO_SYNC = "isToggleAutoSync";
	public static final String TOGGLE_OFFLINE_MODE = "isToggleOfflineMode";
	public static final String VALUE_OFF = "valueOff";
	public static final String VALUE_ON = "valueOn";

	public static ToggleButtonSpec parse(final Node node) {
		final ToggleButtonSpec switchSpec = new ToggleButtonSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node,
			        ToggleFormat.VALUE_ON);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setValueOn(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ToggleFormat.VALUE_OFF);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setValueOff(value);
			}
			value = XmlPaserUtil.getAttrValue(node, ToggleFormat.TEXT_ON);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setTextOn(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ToggleFormat.TEXT_OFF);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setTextOff(value);
			}
			
			value = XmlPaserUtil.getAttrValue(node, ToggleFormat.DISABLE_GROUP);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setDisableGroupID(value);
			}
			
			value = XmlPaserUtil.getAttrValue(node,
			        ToggleFormat.TOGGLE_OFFLINE_MODE);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setToggleOfflineMode(value.equalsIgnoreCase("Y"));
			}
			
			value = XmlPaserUtil.getAttrValue(node,
			        ToggleFormat.TOGGLE_AUTO_SYNC);
			if (!TextUtils.isEmpty(value)) {
				switchSpec.setToggleAutoSync(value.equalsIgnoreCase("Y"));
			}
			
			value = XmlPaserUtil.getAttrValue(node, ToggleFormat.IS_CHECKED);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("Y")) {
					switchSpec.setChecked(true);
				}
			}
		}
		return switchSpec;
	}
}
